package com.tjcd.project.library_base.common.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.LogUtils;
import com.tjcd.project.library_base.R;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/12
 * @Description
 * @SpittingGrooves
 */
public class SharePop extends PopupWindow {

    /**
     * 布局加载器
     */
    private LayoutInflater mInflater;
    /**
     * 上下文
     */
    private Context mcontext;
    /**
     * 弹窗
     */
    private View popupView;
    /**
     * 最终图片
     */
//    private UMImage mThumb;
    /**
     * 图片路径
     */
    private String mImgUrl;
    /**
     * 本地图片
     */
    private int mImgLoacl;
    /**
     * 地址
     */
    private String mUrl;
    /**
     * 标题
     */
    private String mTitle;
    /**
     * 描述
     */
    private String mIntroduce;

    public SharePop(Context context) {
        this.mcontext = context;
        this.mInflater = LayoutInflater.from(context);
        popupView = mInflater.inflate(
                R.layout.common_share_pop, null);
        this.setContentView(popupView);

        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setAnimationStyle(R.style.PopupWindowBottimAnimation);
        this.update();
    }

    public void setShowMsg(String url, String imgUrl, String title, String introduce) {
        this.mUrl = url;
        this.mImgUrl = imgUrl;
        this.mTitle = title;
        this.mIntroduce = introduce;
    }

    public void setShowMsg(String url, int imgLoacl, String title, String introduce) {
        this.mUrl = url;
        this.mImgLoacl = imgLoacl;
        this.mTitle = title;
        this.mIntroduce = introduce;
        LogUtils.e("分享的url" + url);
    }


    public void show() {
        backgroundAlpha(0.6f);
        showAtLocation(((Activity) mcontext).findViewById(android.R.id.content), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1.0f);
    }

    /**
     * 改变背景透明度
     *
     * @param bgAlpha 透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mcontext).getWindow().getAttributes();
        // 0.0-1.0
        lp.alpha = bgAlpha;
        ((Activity) mcontext).getWindow().setAttributes(lp);
    }

//    @OnClick({R.id.share_wechat_friend_tv, R.id.share_wechat_circle_of_friends_tv, R.id.share_qq_friend_tv, R.id.share_qq_space_tv, R.id.share_cancel_tv})
//    public void onViewClicked(View view) {
//        if (TextUtils.isEmpty(mUrl) && view.getId() != R.id.share_cancel_tv) {
//            ToastUtils.showLong("请先完善分享信息");
//            return;
//        }
//        switch (view.getId()) {
//            case R.id.share_wechat_friend_tv: // 微信好友分享
////                if (UMShareAPI.get(mcontext).isInstall((Activity) mcontext, SHARE_MEDIA.WEIXIN)) {
////                    if (!TextUtils.isEmpty(mImgUrl)) {
////                        mThumb = new UMImage(mcontext, mImgUrl);
////                    } else {
////                        mThumb = new UMImage(mcontext, 0 != mImgLoacl ? mImgLoacl : R.drawable.share_default);
////                    }
////                    ShareUtils.Share_Web_WechatFriends((Activity) mcontext, this, mUrl, mThumb, mTitle, mIntroduce);
////                } else {
////                    ToastUtils.showLong("请先安装微信");
////                }
//                dismiss();
//                break;
//            case R.id.share_wechat_circle_of_friends_tv: // 微信朋友圈分享
////                if (UMShareAPI.get(mcontext).isInstall((Activity) mcontext, SHARE_MEDIA.WEIXIN)) {
////                    if (!TextUtils.isEmpty(mImgUrl)) {
////                        mThumb = new UMImage(mcontext, mImgUrl);
////                    } else {
////                        mThumb = new UMImage(mcontext, 0 != mImgLoacl ? mImgLoacl : R.drawable.share_default);
////                    }
////                    ShareUtils.Share_Web_WechatFriendsRing((Activity) mcontext, this, mUrl, mThumb, mTitle, mIntroduce);
////                } else {
////                    ToastUtils.showLong("请先安装微信");
////                }
//                dismiss();
//                break;
//            case R.id.share_qq_friend_tv: // QQ好友分享
////                if (UMShareAPI.get(mcontext).isInstall((Activity) mcontext, SHARE_MEDIA.QQ)) {
////                    if (!TextUtils.isEmpty(mImgUrl)) {
////                        mThumb = new UMImage(mcontext, mImgUrl);
////                    } else {
////                        mThumb = new UMImage(mcontext, 0 != mImgLoacl ? mImgLoacl : R.drawable.share_default);
////                    }
////                    ShareUtils.Share_Web_QQFriends((Activity) mcontext, this, mUrl, mThumb, mTitle, mIntroduce);
////                } else {
////                    ToastUtils.showLong("请先安装QQ");
////                }
//                dismiss();
//                break;
//            case R.id.share_qq_space_tv: // QQ空间分享
////                if (UMShareAPI.get(mcontext).isInstall((Activity) mcontext, SHARE_MEDIA.QQ)) {
////                    if (!TextUtils.isEmpty(mImgUrl)) {
////                        mThumb = new UMImage(mcontext, mImgUrl);
////                    } else {
////                        mThumb = new UMImage(mcontext, 0 != mImgLoacl ? mImgLoacl : R.drawable.share_default);
////                    }
////                    ShareUtils.Share_Web_QQFriendsRing((Activity) mcontext, this, mUrl, mThumb, mTitle, mIntroduce);
////                } else {
////                    ToastUtils.showLong("请先安装QQ");
////                }
//                dismiss();
//                break;
//            case R.id.share_cancel_tv: // 取消
//                dismiss();
//                break;
//        }
//    }

    /**
     * implements UMShareListener  接口实现
     */
//    @Override
//    public void onStart(SHARE_MEDIA share_media) {
//
//    }
//
//    @Override
//    public void onResult(SHARE_MEDIA share_media) {
//        ToastUtils.showLong("分享成功");
//    }
//
//    @Override
//    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
//
//    }
//
//    @Override
//    public void onCancel(SHARE_MEDIA share_media) {
//
//    }
}
